import express from "express";
import { createServer } from "http";
import { Server } from "socket.io";
import adminSocketServer from './socketServer/adminSocketServer'
import nodeSocketServer from './socketServer/nodeSocketServer'

import keyChunkApi from './api/keyChunk'
import nodeComputerApi from './api/nodeComputer'
import errorMiddleware from './errorMiddleware'

const PORT = process.env.PORT

const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
  cors: {
    origin: "*"
  }
});

// 节点计算机
io.of('/nodes').on('connection', nodeSocketServer)

// 数据中心的 前台
io.of('/admin').on("connection", adminSocketServer);

app.use(express.json())

// app.get('/ping', async (req, res, next) => {
//   res.sendStatus(123)
// })

app.use('/api/key-chunk', keyChunkApi)
app.use('/api/node-computer', nodeComputerApi)

app.use(errorMiddleware)

httpServer.listen(PORT, () => {
  console.log(`listen port ${PORT}`);
})